﻿using CL.Game.BLL;
using CL.Redis.BLL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CL.Mobile
{
    public partial class PostMsg : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //必须发送post请求, posttype比填参数, 请求业务类型, 其他参数自定义
            if (HttpContext.Current.Request.RequestType == "POST")
            {
                if("getcode" == Request["posttype"].ToString())
                {
                    string phoneNum = Request["phoneNum"].ToString();
                    var reg = new Regex("^1[34578]\\d{9}$");
                    var mg = reg.Match(phoneNum);
                    if(mg.Success)
                    {
                        var bll = new UsersBLL();
                        //检测当前手机号码是否注册过
                        var userInfo = bll.QueryEntityByUserMobile(phoneNum);
                        if(null != userInfo)
                        {
                            ReturnMsg("当前手机号码已注册过!");
                        }
                        else
                        {
                            var Entity = new BaseRedis().VerifyTokenRedis(string.Empty, "web");
                            if (null != Entity)
                            {
                                var result = bll.SendVerifyCode("web", Entity.Token, phoneNum, 0);
                                if (0 == result.Code)
                                {
                                    ReturnMsg("ok");
                                }
                                else
                                {
                                    ReturnMsg(result.Msg);
                                }
                            }
                            else
                            {
                                ReturnMsg("获取token失败!!");
                            }
                        }
                    }
                    else
                    {
                        ReturnMsg("手机号码不正确!!");
                    }
                }
                else
                {
                    ReturnMsg("请求参数错误!!");
                }
            }
        }

        protected void ReturnMsg(string strMsg)
        {
            Response.Write(strMsg);
            Response.End();
        }
    }
}